package com.honeywen.mymvc.controller;

import com.google.common.collect.Lists;
import com.honeywen.mymvc.entity.Course;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.spi.http.HttpContext;
import java.util.*;
import java.util.stream.Collectors;


/**
 * Created by wangwei on 2018/4/23.
 */
@Controller
@RequestMapping("/hello")
public class HelloController {


    @RequestMapping(value = "/a", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        return "hello  jjj ";
    }

    @RequestMapping("/page1")
    public String page1() {
        return "page1";
    }

    @RequestMapping("/code")
    public String handle(String kap, HttpServletRequest request) {
        System.out.println("input kap:-->" + kap);
        Enumeration<String> names = request.getSession().getAttributeNames();
        while (names.hasMoreElements()) {
            String key = names.nextElement();
            System.out.println(key + " ---> " + request.getSession().getAttribute(key));
        }
        String value = (String) request.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        if (!Objects.equals(kap, value)) {
            System.out.println("=====================" + kap + ", value-->" + value + "==================");
        }


        return "redirect:/hello/page1";

    }


    @RequestMapping("/map")
    @ResponseBody
    public Map<Integer, List<Course>> listMap(HttpServletResponse response, HttpServletRequest  request) {
        response.addHeader("Access-Control-Allow-Origin", "*");

        Course eng = new Course(1001, "eng", 80);
        Course chi = new Course(1001, "chi", 75);

        Course chi2 = new Course(1002, "chi", 77);

        Course eng3 = new Course(1003, "eng", 105);
        Course chi3 = new Course(1003, "chi", 110);

        List<Course> courseList = Lists.newArrayList(eng, chi, chi2, eng3, chi3);
        Map<Integer, List<Course>> map = courseList.stream().collect(Collectors.groupingBy(Course::getUserId));

        System.out.println(map);

        List<Integer> gradeList = map.values().stream().map(i -> i.stream().map(Course::getGrade).reduce(0, Integer::sum)).collect(Collectors.toList());
        System.out.println(gradeList);

        Map<Integer, List<Course>> sortedMap = map.entrySet().stream().sorted(Comparator.comparing(i -> i.getValue().stream().map(Course::getGrade).reduce(0, Integer::sum)))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));


        System.out.println(sortedMap);

        return sortedMap;


    }


    @RequestMapping("/index")
    public String toIndex(){
        return "index";

    }

    @RequestMapping("/crud")
    public String crud() {
        return "crud";
    }

    @RequestMapping("/demo")
    public String demo() {
        return "demo";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/file")
    public String upload() {
        return "file";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String fileUpload(MultipartFile file, HttpServletRequest request) {
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());

        return "redirect:/hello/file";

    }
}
